package jd.se.message;

import jd.se.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageService {
    public void insertMessage(Message mess) {
        SqlSession sess = Utils.openSession();
        try{
            MessageDao dao = sess.getMapper(MessageDao.class);
            dao.insertMessage(mess);
            sess.commit();
        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("新增教师信息失败！",e);
        }finally {
            sess.close();
        }
    }

    public Map<String,Object> getMessage(String to_id){
        SqlSession sess = Utils.openSession();
        MessageDao infoDao = sess.getMapper(MessageDao.class);
        List<MessageTempDto> messageTempList = infoDao.showMessage(to_id);
        List<MessageDto> messageList = new ArrayList<>();
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置格式
        for(MessageTempDto mitem : messageTempList){             //获得带格式的字符串
            messageList.add(new MessageDto(mitem.getU_name(),mitem.getMessage(), format.format(mitem.getTime())));
        }
        Map<String, Object> messages = new HashMap<>();
        messages.put("list", messageList);
        return messages;
    }
}
